import { Component, ViewChild } from '@angular/core';
import { Router } from '@angular/router';
import { DataTableComponent, DialogService, DrawerService, IDrawerOpenResult, TableWidthConfig } from 'ng-devui';
import { SharedMethodService } from 'src/app/@core/services/Sharedmethod/shared-method.service';
import { StorehouseService } from 'src/app/@core/services/storehouse/storehouse.service';
import { LogComponent } from 'src/app/@shared/components/log/log.component';
import { environment } from 'src/environments/environment';

@Component({
  selector: 'app-returning-materials-to-production',
  templateUrl: './returning-materials-to-production.component.html',
  styleUrls: ['./returning-materials-to-production.component.scss']
})
export class ReturningMaterialsToProductionComponent {
  msgs: Array<Object> = [];
  tableData: any[] = [
    {}
  ]
  rangeDate = [
  ];
  StartDate: string = null;
  EndDate: string = null;
  dataTableOptions = {
    columns: [
      {
        field: '退料单号',
        header: '退料单号',
        fieldType: 'text',
        fixedLeft: '230px'
      },

      {
        field: '状态',
        header: '状态',
      },
      {
        field: '审核',
        header: '审核',
      },
      {
        field: '二维码',
        header: '二维码',
      },
      {
        field: '创建人',
        header: '创建人',
        fieldType: 'text',
      },
      {
        field: '创建日期',
        header: '创建日期',
      },
      {
        field: '日志',
        header: '日志',
      },
      // {
      //   field: '审核人',
      //   header: '审核人',
      //   fieldType: 'text',
      // },
      // {
      //   field: '审核日期',
      //   header: '审核日期',
      //   fieldType: 'text',
      // }
    ]
  }
  tableWidthConfig: TableWidthConfig[] = [
    {
      field: 'checkbox',
      width: '80px'
    },
    {
      field: '操作',
      width: '150px'
    },
    {
      field: '发料单号',
      width: '150px'
    },
    {
      field: '状态',
      width: '100px'
    },
    {
      field: '审核',
      width: '100px'
    },
    {
      field: '二维码',
      width: '100px'
    },
    {
      field: '创建人',
      width: '100px'
    },
    {
      field: '创建日期',
      width: '150px'
    },

    {
      field: '日志',
      width: '100px'
    },
    // {
    //   field: '审核人',
    //   width: '100px'
    // },
    // {
    //   field: '审核日期',
    //   width: '150px'
    // }
  ];
  checkedIds = [];
  pager = {
    total: 20,
    pageIndex: 1,
    pageSize: 20
  };
  pageSizeOptions = [10, 20, 30, 50, 100];
  jobOrderReturnNum = ""
  productJob = []
  @ViewChild(DataTableComponent, { static: true }) datatable: DataTableComponent;
  isIconRotated = false;
  constructor(private SharedMethodService: SharedMethodService, private StorehouseService: StorehouseService, private DialogService: DialogService, private Router: Router, private DrawerService: DrawerService) { }
  ngOnInit(): void {
    this.StorehouseService.GetAllJobOrderReturn(this.jobOrderReturnNum, this.StartDate, this.EndDate, this.pager.pageIndex, this.pager.pageSize).subscribe((data: any) => {
      this.tableData = data.items
      this.pager.total = data.totalCount
    })
  }
  rangeDateChange(event: any) {
    this.rangeDate = event;
    this.StartDate = this.rangeDate[0].toLocaleDateString('zh-CN', { year: 'numeric', month: '2-digit', day: '2-digit' }).replace(/\//g, '/');
    this.EndDate = this.rangeDate[1].toLocaleDateString('zh-CN', { year: 'numeric', month: '2-digit', day: '2-digit' }).replace(/\//g, '/');

  }//日期范围选择
  pageIndexChange($event: any) {
    this.StorehouseService.GetAllJobOrderReturn(this.jobOrderReturnNum, this.StartDate, this.EndDate, this.pager.pageIndex, this.pager.pageSize).subscribe((data: any) => {
      this.tableData = data.items
      this.pager.total = data.totalCount
    })
  }//切换页码
  pageSizeChange($event: any) {
    this.StorehouseService.GetAllJobOrderReturn(this.jobOrderReturnNum, this.StartDate, this.EndDate, this.pager.pageIndex, this.pager.pageSize).subscribe((data: any) => {
      this.tableData = data.items
      this.pager.total = data.totalCount
    })
  }//改变每页显示数量
  showToast(type: any, summary: any, content: any) {
    switch (type) {
      default:
        this.msgs = [{ severity: type, summary: summary, content: content }];
    }
  }//提示框
  cleared() {
    this.SharedMethodService.cleared(this.checkedIds, this.tableData, this.datatable).subscribe((data: any) => {
      this.checkedIds = data;
    })
  }
  onRowCheckChange(checked: any, rowIndex: any, nestedIndex: any, rowItem: { $checked: any; $halfChecked: boolean; }) {
    this.SharedMethodService.onRowCheckChange(checked, rowIndex, nestedIndex, rowItem, this.datatable)
  }//单选
  getCheckedRows() {
    this.SharedMethodService.getCheckedRows(this.datatable, 'jobOrderReturnId').subscribe((data: any) => {
      this.checkedIds = data;
    })
  }//获取选中行
  search() {
    this.StorehouseService.GetAllJobOrderReturn(this.jobOrderReturnNum, this.StartDate, this.EndDate, this.pager.pageIndex, this.pager.pageSize).subscribe((data: any) => {
      this.tableData = data.items
      this.pager.total = data.totalCount
    })
  }
  reset() {
    this.jobOrderReturnNum = ""
    this.StartDate = null
    this.EndDate = null
    this.rangeDate = []
    this.StorehouseService.GetAllJobOrderReturn(this.jobOrderReturnNum, this.StartDate, this.EndDate, this.pager.pageIndex, this.pager.pageSize).subscribe((data: any) => {
      this.tableData = data.items
      this.pager.total = data.totalCount
    })
  }
  delete(id: any, dialogtype?: string) {
    const results = this.DialogService.open({
      title: '删除单据',
      content: '确认删除该条单据？',
      dialogtype: dialogtype,
      onClose: () => {
      },
      buttons: [
        {
          cssClass: 'stress',
          text: '确定',
          handler: ($event: Event) => {
            this.StorehouseService.DeleteJobOrderReturn(id).subscribe((data: any) => {
              this.StorehouseService.GetAllJobOrderReturn(this.jobOrderReturnNum, this.StartDate, this.EndDate, this.pager.pageIndex, this.pager.pageSize).subscribe((data: any) => {
                this.tableData = data.items
                this.showToast('success', '成功', '删除成功')
              })
              results.modalInstance.hide();
            }, (error: any) => {
              this.showToast('error', '失败', '删除失败')
            }
            )
          },
        },
        {
          id: 'btn-cancel',
          cssClass: 'common',
          text: '取消',
          handler: ($event: Event) => {
            results.modalInstance.hide();
          },
        },
      ],
    });

  }
  detailed(id: any) {
    this.Router.navigate(['/pages/inventory/returning-materials-detail', id]);
  }
  ruku(id: any) {
    this.StorehouseService.JobOrderReturn(id).subscribe(
      (data: any) => {
        this.showToast('success', '成功', '入库成功')
        this.StorehouseService.GetAllJobOrderReturn(this.jobOrderReturnNum, this.StartDate, this.EndDate, this.pager.pageIndex, this.pager.pageSize).subscribe((data: any) => {
          this.tableData = data.items
          this.pager.total = data.totalCount
        })
      }, (error: any) => {
        this.showToast('error', '失败', error.error||'服务器连接失败')
      }
    )
  }
  isAudit(rowItem: any) {
    this.StorehouseService.AuditJobOrderReturn(rowItem.jobOrderReturnId).subscribe(
      (data: any) => {
        this.showToast('success', '成功', '审核成功')
        this.StorehouseService.GetAllJobOrderReturn(this.jobOrderReturnNum, this.StartDate, this.EndDate, this.pager.pageIndex, this.pager.pageSize).subscribe((data: any) => {
          this.tableData = data.items
          this.pager.total = data.totalCount
        })
      }, (error: any) => {
        this.showToast('error', '失败', error.error)
      }
    )
  }
  results: IDrawerOpenResult;
  GetLogs() {
    this.StorehouseService.GetAllJobOrderReturnLogs(1, 9999).subscribe((data: any) => {
      const logData = data.items
      const columns = [
        {
          field: '生产退料单号',
          header: '生产退料单号',
          fieldType: 'text'
        },
        {
          field: 'content',
          header: '日志内容',
          fieldType: 'text'
        },
        {
          field: '执行人',
          header: '执行人',
          fieldType: 'text'
        },
        {
          field: '执行时间',
          header: '执行时间',
          fieldType: 'text'
        }
      ]
      this.results = this.DrawerService.open({
        drawerContentComponent: LogComponent,
        width: '40%',
        data: { data: logData, columns: columns },
        zIndex: 1000,
        isCover: false,
        fullScreen: true,
        backdropCloseable: true,
        escKeyCloseable: true,
        position: 'right',
        resizable: true,
        onClose: () => {
        },
      });
    })
  }
  GetLogById(rowItem: any) {
    this.StorehouseService.GetLogsByJobOrderReturnId(rowItem.jobOrderReturnId, 1, 9999).subscribe((data: any) => {
      const logData = data.items
      const columns = [
        {
          field: '生产退料单号',
          header: '生产退料单号',
          fieldType: 'text'
        },
        {
          field: 'content',
          header: '日志内容',
          fieldType: 'text'
        },
        {
          field: '执行人',
          header: '执行人',
          fieldType: 'text'
        },
        {
          field: '执行时间',
          header: '执行时间',
          fieldType: 'text'
        }
      ]

      this.results = this.DrawerService.open({
        drawerContentComponent: LogComponent,
        width: '40%',
        data: { data: logData, columns: columns },
        zIndex: 1000,
        isCover: false,
        fullScreen: true,
        backdropCloseable: true,
        escKeyCloseable: true,
        position: 'right',
        resizable: true,
        onClose: () => {

        },
      });
    })
  }
  edit(id: any) {
    this.Router.navigate(['/pages/inventory/edit-returning-materials-to-production', id]);
  }
  getImagesArray2(fileURL: string): string[] {
        let urls = fileURL.split(',');
        urls = urls.map(url => environment.modifiedApiUrl +'/' + url);
        return urls;
      }
}
